<?php

class SimpleApp implements Base\AppConfiguration
{
    public $store = 43;
    public $router = NULL;
    public $view = NULL;
    public $requestParser = NULL;
    public $internalRedirectRequestFilter = FALSE;
    public $preRoutingFilters = FALSE;
    
    function createRequestParser()
    {
        if( $this->requestParser === 'FaultyRoutes' )
        {
            return new TestRequestParserFaultyRoutes();
        }
        
        return new TestRequestParser();
    }
    
    public function createRouter( Core\MVCRequest $request )
    {
        switch( $this->router )
        {
            case 'InRedirectController':
                return new TestInRedirectControllerRouter( $request );
            
            case 'FaultyAction':
                return new TestFaultyActionRouter( $request );
            
            case 'ExceptionInAction':
                return new TestExceptionInActionRouter( $request );
                
            case 'FatalInFatal':
                return new TestFatalInFatalRouter( $request );
                
            case 'EndlessInRedirect':
                return new TestEndlessInRedirectRouter( $request );
             
            case 'EmptyResultController':
                return new TestEmptyResultControllerRouter( $request );
            
            default:
                return new testRouter( $request );
        }
    }
    
    function createView( Implementation\RoutingInformation $routeInfo, Core\MVCRequest $request, Entity\MvcResult $result )
    {
        if( $this->view === 'ExceptionView' AND ! isset( $result->variables['fatal'] ) )
        {
            return new TestExceptionView( $request, $result );
        }
        
        return new TestView( $request, $result );
    }
    
    function createResponseWriter( Implementation\RoutingInformation $routeInfo, Core\MVCRequest $request, Entity\MvcResult $result, Entity\Response $response )
    {
        $responseWriter = new TestResponseWriter( $response );
        $responseWriter->config = $this;
        
        return $responseWriter;
    }
    
    function createFatalRedirectRequest( Core\MVCRequest $request, Entity\MvcResult $result, \Exception $e )
    {
        $request = new Core\MVCRequest();
        $request->uri = '/fatal';
        
        return $request;
    }
    
    function getResponse()
    {
        return $this->response;
    }
    
    function runPreRoutingFilters( Core\MVCRequest $request )
    {
        if( $this->preRoutingFilters === TRUE )
        {
            $request->variables['newVars'] = 'Yes, we did pre routing filter !';
        }
    }
    
    function runRequestFilters( Implementation\RoutingInformation $routeInfo, Core\MVCRequest $request )
    {
        if( $this->internalRedirectRequestFilter === TRUE )
        {
            $inRedirect = new Entity\InRedirect();
            $inRedirect->request = new core\MVCRequest;
            $this->internalRedirectRequestFilter = FALSE;
            $inRedirect->request->variables['request_filter'] = TRUE;
            
            return $inRedirect;
        }
        
        if( $this->internalRedirectRequestFilter === 'exception' )
        {
            return new Entity\InRedirect( $request );
        }
    }
    
    function runResultFilters( Implementation\RoutingInformation $routeInfo, Core\MVCRequest $request, Entity\MvcResult $result ) { }
    
    function runResponseFilters( Implementation\RoutingInformation $routeInfo, Core\MVCRequest $request, Entity\MvcResult $result, Entity\Response $response ) { }
}
